<?php
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'autoload.php';

use payClass\Datebase;
use payClass\QRcode;
use payClass\pay\Config;

/**
 * 扫码支付
 */
class Scan
{
	public static function scanDo()
	{
		$db     = new Datebase();

		$config = new Config();

		$param = $db->param();

		// 订单号
		$param['orderNumber'] = 'C' . time() . 'U' . $param['uid'];

		$param['paytype'] = Config::$folder.'-'.Config::$merchant;
		// 数据检测
		// $verifyRes = $db->verifyScan($param);

		// if ($verifyRes != 'Y') $db->ajaxReturn($verifyRes);

		$data = [
			'version'      => 'v1.1',
			'appid'        => Config::$merchant,
			'out_trade_no' => $param['orderNumber'],
			'amount'       => sprintf("%.2f", $param['price']),
			'out_uid'      => $param['uid'],
			'callback_url' => Config::$notifyUrl,
			'pay_type'     => $param['scanType'],
			'return_type'  => 'app'
		];
		// 生成签名
		$data['sign'] = Config::getSignMsg($data);
		// 发送请求
		$curlRes      = Config::setHttpRequest(Config::$subUrl . '?format=json', json_encode($data));
		// JSON解码
		$resArray     = json_decode($curlRes, true);

		if ($resArray['code'] != '200') $db->ajaxReturn($resArray['msg']);

		header('Location: '.$resArray['url']);
	}
}

Scan::scanDo();
